package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activities")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result listActivity(String name,
                              Integer channel,
                              Integer type,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Activity> pageResult = activityService.listActivity(name,channel,type,page,pageSize);
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除id为{}的活动",id);
        activityService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result addActivity(@RequestBody Activity activity){
        log.info("新增活动：{}",activity);
        activityService.addActivity(activity);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id为{}的活动",id);
        Activity activity = activityService.getById(id);
        return Result.success(activity);
    }
    @PutMapping
    public Result updateActivity(@RequestBody Activity activity){
        log.info("更新活动：{}",activity);
        activityService.updateActivity(activity);
        return Result.success();
    }
    @GetMapping("/type/{type}")
    public Result listActivityByType(@PathVariable Integer type){
        log.info("查询类型为{}的活动",type);
        List<Activity> activities = activityService.listActivityByType(type);
        return Result.success(activities);
    }
}
